package org.example.bussiness;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.ExtensionElement;
import org.flowable.engine.*;
import org.springframework.stereotype.Service;

import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author hsj
 * @description:
 * @date 2023/2/15 14:58
 */
@Service
@AllArgsConstructor
public class IWfProcessServiceImpl implements IWfProcessService{

    private final RuntimeService runtimeService;
    private final IdentityService identityService;
    private final HistoryService historyService;
    private final TaskService taskService;
    private final RepositoryService repositoryService;

    private final IWfExpressionService wfExpressionService;

    /**
     * 获取指定节点的用户
     *
     * @param processDefId 流程定义id
     * @param processInsId 流程实例id
     * @param nodeId       节点id
     */
    @Override
    public WfTaskUser getTaskUser(String processDefId, String processInsId, String nodeId) {
        WfTaskUser taskUser = new WfTaskUser();

        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefId);
        if (bpmnModel == null) {
            return taskUser;
        }

        List<ExtensionElement> elements = WfModelUtil.getUserTaskExtensionElements(nodeId, bpmnModel, "assignee");
        if (elements == null) {
            elements = WfModelUtil.getSubProcessExtensionElements(nodeId, bpmnModel, "assignee"); // 子流程
            if (elements == null) {
                return taskUser;
            }
        }

        // 人员users - 多个 请赋值candidateUserIds，表示多个候选人
        // 角色/部门/职位roles 请赋值candidateGroupIds，表示多个候选组
        // 人员user - 单个 请赋值assignee，表示审核人唯一。！！赋值assignee后，候选组和候选人将失效！！

        LinkedHashSet<String> candidateUserIds = new LinkedHashSet<>(); // 候选用户集合
        LinkedHashSet<String> candidateGroupIds = new LinkedHashSet<>(); // 候选组集合
        String assignee = null; // 唯一审核人

        for (ExtensionElement element : elements) {
            String type = element.getAttributes().get("type").get(0).getValue();
            String value = element.getAttributes().get("value").get(0).getValue();

            switch (type) {
                case "roles": // 角色/部门/职位
                    candidateGroupIds.addAll(Func.toStrList(value));
                    break;
                case "users": // 用户s
                    candidateUserIds.addAll(Func.toStrList(value));
                    break;
                case "user": // 用户
                    assignee = value;
                    break;
                case "custom": // 自定义
                    if (StringUtils.isBlank(processInsId)) {
                        break;
                    }
                    switch (value) {
                        case "initiator":
                            // 申请人已经默认的流程变量：initiator，参见启动流程实例
//                            assignee = (String) runtimeService.getVariable(processInsId,"initiator");
                            break;
                    }
                    break;
            }
        }
        taskUser.setAssignee(assignee);
        taskUser.setCandidateUserIds(candidateUserIds);
        taskUser.setCandidateGroupIds(candidateGroupIds);

        return taskUser;
    }
}
